<template>
	<div>
		<jk-dialog
			v-model="showDialog"
			title="翻改任务"
			width="1280px"
			:show-content-loading="showLoading"
			:confirm-btn-loading="saveBtnLoading"
			:show-confirm-btn="pageType !== 'detail'"
			:show-cancel-btn="pageType !== 'detail'"
			:show-close-btn="pageType === 'detail'"
			@on-close="onVisibleChange(false)"
			@on-cancel="onVisibleChange(false)"
			@on-visible-change="onVisibleChange"
			@on-confirm="onConfirmBtn"
		>
			<div ref="box1" class="flex-between-center-wrap">
				<div class="flex-start-center-wrap">
				</div>
				<div class="flex-end-center-wrap">
					<jk-search-input v-model="moreSearchParams.batchCode" placeholder="批号" size="small" class="search-bar-item-code margin-5-0 item-gap" clearable />
					<jk-search-input v-model="searchParams.productNameOrCode" placeholder="品种名称" size="small" class="search-bar-item-size3 margin-5-0 item-gap" clearable />
					<jk-search-input v-model="searchParams.machineNameOrCode" placeholder="机台名称" size="small" class="search-bar-item-size2 margin-5-0 item-gap" clearable />
					<jk-search-button class="" @click="onSearchBtn"></jk-search-button>
				</div>
			</div>
			<jk-table
				ref="jkTable"
				show-overflow
				border
				:height="dialogTableHeight - 10"
				:loading="loading"
				:data="tableData"
				:checkbox-config="{checkMethod: checkboxMethod}"
				@checkbox-change="onCheckBoxChange"
			>
				<vxe-column type="checkbox" align="center" :width="44"></vxe-column>
				<vxe-column field="machineName" align="left" :width="80" title="机台"></vxe-column>
				<vxe-column title="状态" align="center" field="unitPrice" :width="80">
					<template slot-scope="scope">
						<jk-state-bar :state="scope.row.openingState" label-key="operationName" :state-list="openStateList">{{ scope.row.paperTubeList ? scope.row.paperTubeList.map(x => x.name).join(',') : '' }}</jk-state-bar>
					</template>
				</vxe-column>
				<vxe-column field="productName" :width="180" title="品种"></vxe-column>
				<vxe-column field="batchCode" align="left" :min-width="140" title="批号"></vxe-column>
				<vxe-column field="preProductName" :width="180" title="改前品种"></vxe-column>
				<vxe-column field="preBatchCode" align="left" :min-width="140" title="改前批号"></vxe-column>
				<vxe-column title="纸管颜色" align="left" field="unitPrice" :min-width="100">
					<template slot-scope="scope">
						<span>{{ scope.row.paperTubeList ? scope.row.paperTubeList.map(x => x.name).join(',') : '' }}</span>
					</template>
				</vxe-column>
				<vxe-column title="腰绳颜色" align="left" field="waistPopeList" :min-width="100">
					<template slot-scope="scope">
						<span>{{ scope.row.waistPopeList ? scope.row.waistPopeList.map(x => x.name).join(',') : '' }}</span>
					</template>
				</vxe-column>
				<vxe-column title="标签颜色" align="left" field="unitPrice" :min-width="100">
					<template slot-scope="scope">
						<span>{{ scope.row.labelColorList ? scope.row.labelColorList.map(x => x.name).join(',') : '' }}</span>
					</template>
				</vxe-column>
			</jk-table>
		</jk-dialog>
	</div>
</template>

<script>
    import { pageConfig, formatDate } from '/src/utils/jk/jk-tool';
    import calHeight from '/src/mixins/cal-dialog-height';
    import remoteSearchProductInput from '../../technology/order/components/remote-search-product-input/index';
    import { orderPriorityList } from '../../../utils/jk/status-option-list';
    import { creationChangePlan, subitem, getChangePlanPage, updateProduction } from '@/api/production/planAlterSpinning';
    import {
        productionPrdNoticeMachineChangePostPage
    } from '../../../api/production/xsRenovateOrder';
    import { openStateList } from '../../../utils/jk/status-option-list';
    export default {
        mixins: [calHeight],
        components: { remoteSearchProductInput },
        props: {
            dataItem: {
                type: Object,
                default() {
                    return {};
                }
            },
            pageType: {
                type: String,
                default: ''
            },
            dialogState: {
                type: Boolean,
                default: false
            },
            defaultSelectRows: {
                type: Array,
                default() {
                    return [];
                }
            },
            request: {
                type: Function,
                require: true,
                default: null
            }
        },
        data() {
            return {
                apiItem: {
                    'page': productionPrdNoticeMachineChangePostPage,
                    'post': creationChangePlan,
                    'postzx': subitem,
                    'exportExcel': null
                },
                openStateList: openStateList,
                saveBtnLoading: false,
                showDialog: false,
                showLoading: true,
                priorityList: orderPriorityList,
                searchParams: {
                    pageNo: pageConfig.pageNo,
                    pageSize: pageConfig.pageSize,
                    productOrderCode: '',
                    machineNameOrCode: '',
                    productNameOrCode: '',
                    processCodes: [],
                    batchCode: '',
                    openingState: 1,
                    processCode: '',
                    processId: '',
                    workshopId: null,
                    workshopCode: null
                },
                tableData: [],
                loading: false,
                moreSearchParams: {
                    code: '',
                    prdNoticeCode: '',
                    batchCode: '',
                    priorityId: null,
                    ifTest: false
                },
                workshopList: [],
                processList: [],
                checkArr: [],
                idsVO: {
                    ids: []
                }
            };
        },
        methods: {
            checkboxMethod(e) {
                return !e.row._disabled;
            },
            setCheckState(arr) {
                arr.forEach((x) => {
                    x._disabled = false;
                    this.defaultSelectRows.forEach(k => {
                        if (x.id === k.prdNoticeMachineChangeId) {
                            x._disabled = true;
                        }
                    });
                });
                this.tableData = arr;
                this.tableData.forEach((x, index) => {
                    if (x._disabled) {
                        this.$refs.jkTable.$refs.xTable.setCheckboxRow([this.tableData[index], this.tableData[index]], true);
                    }
                });
            },
            onCheckBoxChange({ records }) {
                this.checkArr = [];
                records.forEach(x => {
                    this.checkArr.push(x);
                    this.idsVO.ids.push(x.id);
                    return x;
                });
            },
            onSearchBtn() {
                this.$set(this.searchParams, 'pageNo', 1);
                this.getListRequest();
            },
            // 打开关闭弹窗
            onVisibleChange(e) {
                this.$emit('on-visible-change', e);
                if (!e) {
                    Object.assign(this.$data, this.$options.data());
                }
            },
            // 弹窗确定
            onConfirmBtn() {
                this.createAndUpdateRequest();
            },
            createAndUpdateRequest() {
                this.apiItem.postzx(this.idsVO).then(res => {
                    if (res.data.status === 200) {
                        this.$emit('itemList', res.data.res);
                        this.$emit('on-visible-change', false);
                        // this.request();
                    }
                });
            },
            getParamDatas() {
                const params = JSON.parse(JSON.stringify(this.dataItem));
                const processItem = this.processList.find(k => k.id === params.processId) || {};
                params.processCode = processItem.code;
                const workshopItem = this.workshopList.find(k => k.id === params.workshopId) || {};
                params.workshopCode = workshopItem.code;
                params.machineNameOrCode = this.searchParams.machineNameOrCode;
                params.productNameOrCode = this.searchParams.productNameOrCode;
                params.productOrderCode = this.searchParams.productOrderCode;
                params.batchCode = this.moreSearchParams.batchCode;
                params.openingState = 1;
                return params;
            },
            /** 查询列表 */
            getListRequest() {
                this.apiItem.page(this.getParamDatas()).then(res => {
                    if (res.data.status === 200) {
                        this.setCheckState(res.data.res || []);
                        this.tableData = res.data.res;
                        this.totalCount = res.data.count;
                    }
                });
            },
            async getDependentData() {
                this.showLoading = true;
                await this.getListRequest();
                this.showLoading = false;
                this.workshopList = this.$store.state.user.userWorkshop.workshopList;
                this.processList = this.$store.state.user.userProcess.processList || [];
            }
        },
        watch: {
            dialogState(newVal) {
                this.showDialog = newVal;
                if (newVal) {
                    this.getDependentData();
                }
            }
        }
    };
</script>
